package com.itheima.exception;

import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @Author 王加鑫
 * @Date 2021/07/27 21:57
 * @Since JDK 1.8
 */
@Component
@ControllerAdvice
public class ProjectExceptionAdvice {

    @ExceptionHandler(BusinessException.class)
    public String doBusinessException(Exception ex, Model model){
        //使用参数Model将要保存的数据传递到页面上，功能等于ModelAndView
        //业务异常出现的消息要发送给用户查看
        model.addAttribute("msg",ex.getMessage());
        return "error.jsp";

    }


    @ExceptionHandler(SystemException.class)
    public String doSystemException(Exception ex, Model model){
        //系统异常出现的消息不要发送给用户查看，发送统一的信息给用户看
        //实际的问题现象应该输出到日志，运维开发人员通过后台系统查看
        model.addAttribute("msg", "服务器出现问题，请联系管理员！");
        ex.printStackTrace();
        return "error.jsp";
    }


    @ExceptionHandler(Exception.class)
    public String doException(Exception ex, Model m) {
        m.addAttribute("msg", ex.getMessage());
        ex.printStackTrace();
        return "error.jsp";
    }


}
